set hive.exec.dynamic.partition=true;--动态分区
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.max.dynamic.partitions=91;--每天生成 91 个分区
set hive.exec.max.dynamic.partitions.pernode=91;--每天生成 91 个分区
-- 订单更新量较小，不采取 （1天ODS+29天DWD）方式递归计算；直接取30天ODS数据去重
with mark_expand_new as (
    select
     ID
    ,WAYBILL_ID
    ,TERMINAL_DISPATCH_CODE
    ,NEW_TERMINAL_DISPATCH_CODE
    ,DISPATCH_NETWORK_CODE
    ,DISPATCH_NETWORK_NAME
    ,LAST_CENTER_CODE
    ,LAST_CENTER_NAME
    ,INPUT_TIME
    ,CREATE_TIME
    ,UPDATE_TIME
    ,row_number() over(partition by WAYBILL_ID,INPUT_TIME order by dt desc) as row_id
    ,date_format(input_time, 'yyyy-MM-dd') as dt
    from jms_ods.YL_OMS_ORDER_return
    where dt >= date_add('{{ execution_date | cst_ds }}', -30) and dt <= '{{ execution_date | cst_ds }}'
)

insert overwrite table jms_dwd.dwd_yl_oms_order_return_dt partition(dt)
select
       ID
      ,WAYBILL_ID
      ,TERMINAL_DISPATCH_CODE
      ,NEW_TERMINAL_DISPATCH_CODE
      ,DISPATCH_NETWORK_CODE
      ,DISPATCH_NETWORK_NAME
      ,LAST_CENTER_CODE
      ,LAST_CENTER_NAME
      ,INPUT_TIME
      ,CREATE_TIME
      ,UPDATE_TIME
      ,dt
from  mark_expand_new
where row_id = 1
and  dt >= date_add('{{ execution_date | cst_ds }}', -30) and dt <= '{{ execution_date | cst_ds }}'
distribute by  1;